import { splitMethods, importFromOrigin } from './common.js'

export const formatProps = (str) => {
  if (!str) return ''
  const propsObj = str.replace(/props:((.+\n|\n|.)+)/g, '$1').trim()
  const isPropsList = propsObj.startsWith('[') && propsObj.endsWith(']')
  const propsListStr = propsObj.replace(/^\{(([\s\S]*?)\s*?)\}$/, '$1')
  const list = isPropsList ? new Function('return ' + propsListStr)() : splitMethods(propsListStr)
  if (list.length > 0) {
    importFromOrigin('computed')
  }
  const propsGenerateComputed = list.reduce((pre, cur) => {
    const name = isPropsList ? cur : cur.replace(/(\w+)\s?:\s?\{([\s\S]*?)\s*?\}/, '$1')
    pre += `const ${name} = computed(()=>props.${name})\n`
    return pre
  }, ``)
  return `const props = defineProps(${propsObj})
    ${propsGenerateComputed}`
}
